package com.dao.impl;

import static com.def.GenericDef.*;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dao.IBisMtrlDao;
import com.model.Bis_mtrl;
import com.model.Ret_wh_mtrl;
import com.tx.Xpbismtr.XpbismtrI;
import com.tx.Xpbismtr.XpbismtrIA;
import com.tx.Xpbismtr.XpbismtrO;
import com.tx.Xpbismtr.XpbismtrOA;
import com.util.Util;
 
@Repository
public class BisMtrlHibernateDao extends GenericHibernateDao<Bis_mtrl, String> 
implements IBisMtrlDao
{
	public XpbismtrO queryByCondition(XpbismtrI xpbismtlI){
		/***
		 * 支持
		 *   1.模糊查询
		 *   2.by id 查询
		 *   3.全表查询:if(iary.getMtrl_product_id ==null)
		 *   4.by mtrl_cate查询
		 * **/
		 
		XpbismtrO xpbisqrsO = new XpbismtrO();
		xpbisqrsO.setTrx_id("XPBISMTR");
		xpbisqrsO.setType_id("O");
		List<XpbismtrOA> oaryList = new ArrayList<XpbismtrOA> ();
		
		XpbismtrIA iary = xpbismtlI.getIary().get(0);
		String mtrl_prod_id = iary.getMtrl_prod_id();
		
		String hql ="FROM Bis_mtrl where 1=1 ";
		if(iary.getMtrl_prod_id()!=null){
			if(mtrl_prod_id.indexOf('*')==-1){
				hql = hql + " and mtrl_prod_id='" + mtrl_prod_id + "'";
			}else{
				hql = hql + " and mtrl_prod_id like '" + mtrl_prod_id.replace('*', '%') + "'";
			}
		}
		if(iary.getMtrl_cate()!=null){
			hql = hql + " and mtrl_cate='" + iary.getMtrl_cate() + "'";
		}
		if(iary.getMtrl_mkr()!=null){
			hql = hql + " and mtrl_mkr='" + iary.getMtrl_mkr() + "'";
		}
		List<Bis_mtrl> bisMtrlList =this.find( hql );
		for(Bis_mtrl bis_mtrl : bisMtrlList){
			XpbismtrOA oary = new XpbismtrOA();
			oary.setMtrl_prod_id(bis_mtrl.getMtrl_prod_id());
			oary.setMtrl_prod_dsc(bis_mtrl.getMtrl_prod_dsc());
			oary.setMtrl_cate(bis_mtrl.getMtrl_cate());
			oary.setMtrl_mkr(bis_mtrl.getMtrl_mkr());
			oary.setMtrl_unit(bis_mtrl.getMtrl_unit());
			oary.setMtrl_prod_id_len(bis_mtrl.getMtrl_prod_id_len());
			oary.setMtrl_chg_cate(bis_mtrl.getMtrl_chg_cate());
			oary.setMtrl_use_mode(bis_mtrl.getMtrl_use_mode());
			oary.setMtrl_part(bis_mtrl.getMtrl_part());
			oaryList.add(oary);
		}
		xpbisqrsO.setOary(oaryList);
		xpbisqrsO.setTbl_cnt(oaryList.size());
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	
	public XpbismtrO addProc(XpbismtrI xpbismtrI) throws Exception{
		XpbismtrO xpbismtrO = new XpbismtrO();
		xpbismtrO.setTrx_id("XPBISMTR");
		xpbismtrO.setType_id("O");
		
		XpbismtrIA iary = xpbismtrI.getIary().get(0);
		String mtrl_product_id = iary.getMtrl_prod_id();
		
		Bis_mtrl queryBisQrs = this.get(mtrl_product_id);
		if( queryBisQrs != null){
			xpbismtrO.setRtn_code(E_XPBISQRS_QRS_ALREADY_EXIST+"");
			xpbismtrO.setRtn_mesg("Record already Exist");
			return xpbismtrO;
		}
		Bis_mtrl bis_mtrl = new Bis_mtrl();
		bis_mtrl.setMtrl_prod_id(mtrl_product_id);
		bis_mtrl.setMtrl_prod_dsc(iary.getMtrl_prod_dsc());
		bis_mtrl.setMtrl_cate(iary.getMtrl_cate());
		bis_mtrl.setMtrl_mkr(iary.getMtrl_mkr());
		bis_mtrl.setMtrl_unit(iary.getMtrl_unit());
		bis_mtrl.setMtrl_prod_id_len(iary.getMtrl_prod_id_len());
		bis_mtrl.setMtrl_chg_cate(iary.getMtrl_chg_cate());
		bis_mtrl.setMtrl_use_mode(iary.getMtrl_use_mode());
		bis_mtrl.setMtrl_part(iary.getMtrl_part());
//		bis_mtrl.setFrom_thickness(iary.getFrom_thickness());
		try{
			this.save(bis_mtrl);
		}catch(Exception ex){
			throw ex ;
		}
        xpbismtrO.setRtn_code(RETURN_CODE_OK);
        xpbismtrO.setRtn_mesg("success");
		return xpbismtrO;
	}
	public XpbismtrO updateProc(XpbismtrI xpbismtrI){
		XpbismtrO xpbismtrO = new XpbismtrO();
		xpbismtrO.setTrx_id("XPBISMTR");
		xpbismtrO.setType_id("O");
		
		XpbismtrIA iary = xpbismtrI.getIary().get(0);
		String mtrl_product_id = iary.getMtrl_prod_id();
		
		Bis_mtrl bis_mtrl = this.get(mtrl_product_id);
		if( bis_mtrl == null){
			xpbismtrO.setRtn_code(E_XPBISMTR_NOT_FOUND_MTRL+"");
			xpbismtrO.setRtn_mesg("Record already Exist");
			return xpbismtrO;
		}
		if(iary.getMtrl_prod_dsc()!=null){
			bis_mtrl.setMtrl_prod_dsc(iary.getMtrl_prod_dsc());
		}
		if(iary.getMtrl_cate()!=null){
			bis_mtrl.setMtrl_cate(iary.getMtrl_cate());
		}
		if(iary.getMtrl_mkr()!=null){
			bis_mtrl.setMtrl_mkr(iary.getMtrl_mkr());
		}
		if(iary.getMtrl_unit()!=null){
			bis_mtrl.setMtrl_unit(iary.getMtrl_unit());
		}
		if(iary.getMtrl_prod_id_len()!=null){
			bis_mtrl.setMtrl_prod_id_len(iary.getMtrl_prod_id_len());
		}
		if(iary.getMtrl_chg_cate()!=null){
			bis_mtrl.setMtrl_chg_cate(iary.getMtrl_chg_cate());
		}
		if(iary.getMtrl_use_mode()!=null){
			bis_mtrl.setMtrl_use_mode(iary.getMtrl_use_mode());
		}
		if(iary.getMtrl_part()!=null){
			bis_mtrl.setMtrl_part(iary.getMtrl_part());
		}
//		if(iary.getFrom_thickness()!=null){
//			bis_mtrl.setFrom_thickness(iary.getFrom_thickness());
//		}
        this.update(bis_mtrl);
        
        xpbismtrO.setRtn_code(RETURN_CODE_OK);
        xpbismtrO.setRtn_mesg("success");
		return xpbismtrO;
	}
	public XpbismtrO deleteProc(XpbismtrI xpbismtlI){
		XpbismtrO xpbismtlO = new XpbismtrO();
		xpbismtlO.setTrx_id("XPBISMTR");
		xpbismtlO.setType_id("O");
		XpbismtrIA iary = xpbismtlI.getIary().get(0);
		
		Bis_mtrl bis_mtrl = this.get(iary.getMtrl_prod_id()) ;
		if(bis_mtrl==null){
			xpbismtlO.setRtn_code(E_XPBISMTR_NOT_FOUND_MTRL+"");
			xpbismtlO.setRtn_mesg("not found record");
			return xpbismtlO;
		}
    	this.delete(bis_mtrl);
		

		xpbismtlO.setRtn_code(RETURN_CODE_OK);
		xpbismtlO.setRtn_mesg("success");
		return xpbismtlO;
	}
	public Bis_mtrl getDataByKeyProc(String mtrl_id){
		Bis_mtrl bis_mtrl = this.get(mtrl_id);
		return bis_mtrl;
	}
	
	public XpbismtrO queryFormkr(XpbismtrI xpbismtlI){

		XpbismtrO xpbisqrsO = new XpbismtrO();
		xpbisqrsO.setTrx_id("XPBISMTR");
		xpbisqrsO.setType_id("O");
		List<XpbismtrOA> oaryList = new ArrayList<XpbismtrOA> ();
		
		XpbismtrIA iary = xpbismtlI.getIary().get(0);
		String mtrl_mkr = iary.getMtrl_mkr();
		String mtrl_cate = iary.getMtrl_cate();
		String mtrl_prod_dsc = iary.getMtrl_prod_dsc();
		String sql = "SELECT DISTINCT MTRL_PROD_ID,A.* FROM BIS_MTRL A JOIN BIS_DATA B ON A.MTRL_PROD_ID=B.EXT_1 WHERE 1=1";
		
        if(!Util.isSpaceCheck(mtrl_mkr)){
        	sql = sql + " AND B.DATA_CATE='CSMT' AND B.DATA_ITEM='" + mtrl_mkr + "'";
        }
		if(!Util.isSpaceCheck(mtrl_cate)){
			sql = sql + " AND A.MTRL_CATE='" + mtrl_cate +"'";
		}
		if(!Util.isSpaceCheck(mtrl_prod_dsc)){
			sql = sql + " AND A.MTRL_PROD_DSC LIKE '%" + mtrl_prod_dsc + "%'";
		}

		List<Bis_mtrl> bisMtrlList =this.findBySQL(sql,Bis_mtrl.class);
		for(Bis_mtrl bis_mtrl : bisMtrlList){
			XpbismtrOA oary = new XpbismtrOA();
			oary.setMtrl_prod_id(bis_mtrl.getMtrl_prod_id());
			oaryList.add(oary);
		}
		xpbisqrsO.setOary(oaryList);
		xpbisqrsO.setTbl_cnt(oaryList.size());
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
	
	public XpbismtrO queryFordsc(XpbismtrI xpbismtlI){

		XpbismtrO xpbisqrsO = new XpbismtrO();
		xpbisqrsO.setTrx_id("XPBISMTR");
		xpbisqrsO.setType_id("O");
		List<XpbismtrOA> oaryList = new ArrayList<XpbismtrOA> ();
		
		XpbismtrIA iary = xpbismtlI.getIary().get(0);
		String mtrl_mkr = iary.getMtrl_mkr();
		String mtrl_cate = iary.getMtrl_cate();
		String mtrl_prod_dsc = iary.getMtrl_prod_dsc();
		String dest_shop = iary.getDest_shop();

         String sql = "";
         if(mtrl_prod_dsc!=null){
        	 sql = "SELECT A.* FROM RET_WH_MTRL A JOIN BIS_MTRL B ON A.MTRL_PROD_ID_FK=B.MTRL_PROD_ID WHERE A.CUS_ID_FK='"
            		 + mtrl_mkr + "' AND A.MTRL_CATE='"
            		 + mtrl_cate + "' AND A.DEST_SHOP='"
            		 + dest_shop + "' AND B.MTRL_PROD_DSC LIKE '%"
            		 + mtrl_prod_dsc + "%'";
         }else{
        	 sql = "SELECT * FROM RET_WH_MTRL WHERE CUS_ID_FK='"
        			 + mtrl_mkr + "' AND MTRL_CATE='"
            		 + mtrl_cate + "' AND DEST_SHOP='"
            		 + dest_shop + "'";
         }

		List<Ret_wh_mtrl> retWhMtrlList =this.findBySQL(sql,Ret_wh_mtrl.class);
		for(Ret_wh_mtrl ret_wh_mtrl : retWhMtrlList){
			XpbismtrOA oary = new XpbismtrOA();
			oary.setMtrl_prod_id(ret_wh_mtrl.getMtrl_prod_id_fk());
			oary.setMtrl_cate(ret_wh_mtrl.getMtrl_cate());
			oary.setMtrl_mkr(ret_wh_mtrl.getCus_id_fk());
			oaryList.add(oary);
		}
		xpbisqrsO.setOary(oaryList);
		xpbisqrsO.setTbl_cnt(oaryList.size());
		xpbisqrsO.setRtn_code(RETURN_CODE_OK);
		xpbisqrsO.setRtn_mesg("success");
		return xpbisqrsO;
	}
}
